using System;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using umlungu.engineX.webserviceX;

namespace umlungu.engineX.webserviceX {
	/// <summary>
	/// umlungu Gatekeeper. 
	/// Validation of users for access to engineX internet-based services is managed from within the Gatekeeper.
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class x_webservice : x_result
	{
		#region Invisible properties
		private const string errService	= "error_service";
		private const string errServiceMsg	= "Error loading service from application. Is service object created in the Global.asax?";
		#endregion

		#region Visible properties
		private string id = "x_service";
		/// <summary>The web service identifier</summary>
		/// <value>The identifier string of the x_service object definition file (without .xml extension)</value>
		public string ID 	{	get { return id; }
								set { id = value; }
							}
		#endregion
		
		/// <summary>
		/// Default Constructor for the Gatekeeper Web Service
		/// </summary>
		public x_webservice() : base() {
			ResultType = x_resultType.webservice;
		}

		#region Protected methods
		/// <summary>
		/// Executes a webservice method from the definition list
		/// </summary>
		/// <param name="MethodDSN">The identifier of the DSN, defined in web.config</param>
		/// <param name="MethodName">The name of the method in the defintion list</param>
		/// <param name="MethodArgs">The method argument strings, in the order that they appear in the method defintion</param>
		/// <returns>XmlDocument of the defintion</returns>
		protected void ExecuteMethod(string MethodDSN, string MethodName, params string[] MethodArgs) {
			try {
				x_method methodX = new x_method(Application, id);
				if (MethodDSN != null && MethodDSN != "")
					methodX.DSN = MethodDSN;
				methodX.Load(MethodName);
				Name = methodX.Module.GetAttribute("name");
				AddOk();
				methodX.Call(Result, MethodArgs);
				AddTime();
			} 
			catch(x_exception e)		{AddError(e);}
			catch(System.Exception e) 	{AddError(e);}
		}
		#endregion

		#region Public methods
		/// <summary>
		/// Displays possible errors of the webservice.
		/// </summary>
		/// <returns>
		///	XmlDocument of the defined errors and errorcodes
		/// </returns>
		[WebMethod] public XmlDocument x_ErrorList() {
			try {
				AddOk();
				x_service xServ = (x_service)Application.Get(id);
				if (xServ == null)
					throw new x_exception(errService, errServiceMsg);
				AddNode(xServ.GetErrors());
			} 
			catch(x_exception e)		{AddError(e);}
			catch(System.Exception e) 	{AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Displays the defintion of the webservice.
		/// </summary>
		/// <returns>
		///	XmlDocument of the defintion
		/// </returns>
		[WebMethod] public XmlDocument x_Defintion() {
			try {
				AddOk();
				AddNode("ID", id);
				x_service xServ = (x_service)Application.Get(id);
				if (xServ == null)
					throw new x_exception(errService, errServiceMsg);
				AddNode(xServ.Defintion);
			} 
			catch(x_exception e)		{AddError(e);}
			catch(System.Exception e) 	{AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Displays the database connection string.
		/// </summary>
		/// <returns>
		///	XmlDocument of contain the database DSN
		/// </returns>
		[WebMethod] public XmlDocument x_Connection() {
			x_method methodX = new x_method(Application, Result, id);
			Name = "Connection";
			AddOk();
			if (Debug)
				AddNode("DSN", methodX.DSN);
			using (SqlConnection xdb = new SqlConnection(methodX.DSN)) {
				try {
					AddNode("Message", String.Concat("Conneted to ", xdb.Database , " on ", xdb.DataSource));
					xdb.Open();
				} 
				catch(x_exception e)		{ AddError(e); }
				catch(System.Exception e) 	{ AddError(e); }
				finally						{ xdb.Close(); }
			}	// will force a dispose of the connection
			return(Result);
		}
		#endregion
	}
}
